/**
 * Created by 2017/06/02.
 */
import React from "react";
import Relay from "react-relay";
import Reactkey from "../../../../util/ReactKey";
import Util from "../../../../util/Util";
import { Tabs, message } from 'antd';
import InfoOrderMutation from '../../mutation/InfoOrderMutation';
import InfoEnquiryOrderMutation from '../../mutation/InfoEnquiryOrderMutation';
import FindSampleCreateMutation from '../mutation/FindSampleCreateMutation';

import EventManager from "../../../../util/EventManager"
// import OrderDetailCloth from '../Orderdetail_cloth';
// import OrderDetailRaw from '../Orderdetail_raw';
// import OrderDetailGms from '../Orderdetail_gms';
// import OrderDetailYarn from '../Orderdetail_yarn';
// import OrderDetailPrint from '../Orderdetail_print';
//订单详情
import Index from './order_detail_index/index';
// 订单进度
import OrderProgress from '../../order_progress/OrderProgress';
import FabricRaw from './fabricRaw/FabricRaw';
import Payment from './payment/Payment';
import Receipt from './payment/Receipt';

// 生产进度
import OrderProductionProcess from './production_process/OrderProductionProcess';
// 合同管理
import ContractManagement from './contract_management/index'
//收发货记录
import InOutOrder from './inout_order';
//订单修改记录
import OrderModificationRecord from './order_modification_record'
//物料清单
import MaterielBom from './material_bom/MaterielBom'
import "../css/order_index.css";
//打样进度
import ProofingProgress from './proofing_progress';

const TabPane = Tabs.TabPane;
/*信息列表*/
export default class OrderIndex extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            defaultActiveKey: 1,
            order: [],
            orderType: null,  //订单类型 1:  gms 成衣, 2： raw 辅料,3： cloth 布料, 4： print 印染, 5： yarn 纱线
            orderStatus: [], //订单状态 status  '-1':'待创建',0:'待发布',1:'待接单(已发布)',2:'待签订（已接收）',3:'已签订',4:'交易完成'
            isValid: null,
            orderClassify: null,
            // showProofing: false,
        };
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.serviceType = sessionStorage.getItem('serviceType');
        this.orderId = this.props.location.state.record.oid || '';
        this.enquiryId = this.props.location.state.record.enquiryId || '';
        this.partAName = this.props.location.state.record.partAName || "";
        this.account = this.props.location.state.record.account || "";
        this.callback = this.callback.bind(this)
    }

    componentWillMount(){
        const s = this.props.location.state;
        if(this.enquiryId != '' && this.enquiryId != undefined){
          Relay.Store.commitUpdate(
              new InfoEnquiryOrderMutation({
                  orderId: this.orderId,
                  enId: this.enterpriseId,
                  enquiryId: this.enquiryId
              }),
              {
                  onFailure: () => {
                      message.info('请求失败')
                      this.setState({
                          isLoading: false
                      })
                  },
                  onSuccess: (response) => {
                      let errorCode = response.infoEnquiryOrder.infoEnquiryOrder.errorCode;
                      if(errorCode=='0'){
                          let order = response.infoEnquiryOrder.infoEnquiryOrder.order;
                          this.setState({
                              order: order,
                              orderType: order.orderType,
                              orderStatus: order.status,
                              isValid: order.isValid
                          })
                      } else {
                          message.info('请求失败')
                      }
                  }
              }
          );
        }else{
          Relay.Store.commitUpdate(
              new InfoOrderMutation({
                  orderId: this.orderId,
                  enId: this.enterpriseId,
              }),
              {
                  onFailure: () => {
                      message.info('请求失败')
                      this.setState({
                          isLoading: false
                      })
                  },
                  onSuccess: (response) => {
                      let errorCode = response.infoOrder.infoOrder.errorCode;
                      if(errorCode=='0'){
                          let order = response.infoOrder.infoOrder.order;
                          this.setState({
                              order: order,
                              orderType: order.orderType,
                              orderStatus: order.status,
                              orderClassify: order.orderClassify
                          })
                      } else {
                          message.info('请求失败')
                      }
                  }
              }
          );
        }
        // this.findSampleCreate()
        if(s && s.defaultActiveKey) {
            this.setState({
                defaultActiveKey:s.defaultActiveKey
            })
        } else {
            message.info("请按照规则进入此页面");
        }
    }

    // findSampleCreate() {
    //     Relay.Store.commitUpdate(
    //         new FindSampleCreateMutation({
    //             orderId: this.orderId,
    //         }),
    //         {
    //             onFailure: () => {
    //                 message.info('请求失败')
    //                 this.setState({
    //                     isLoading: false
    //                 })
    //             },
    //             onSuccess: (response) => {
    //                 let errorCode = response.findSampleCreateByOrderId.responseData.errorCode;
    //                 if(errorCode=='0'){
    //                     let sampleCreate = response.findSampleCreateByOrderId.responseData.sampleCreate;
    //                     if (sampleCreate == null) {
    //                         this.setState({
    //                             showProofing: false
    //                         })
    //                     } else {
    //                         this.setState({
    //                             showProofing: true
    //                         })
    //                     }
    //                 } else {
    //                     message.info('请求失败')
    //                 }
    //             }
    //         }
    //     );
    // }
    callback(key) {
        this.props.location.state && this.props.location.state.showProofing ? (
            this.refs.proofing_progress.setState({
                start: 1,//当tab切换的时候，打样进度页面切换为初始页
            })
        ) : null
        //动态移除类名--物料清单-筛选下拉框
        let body = document.getElementsByTagName("body")[0];
        if(key != 9){
            // EventManager.emitter('removeBodyClass',"table_filter_materiel");
            Util.removeClass(body,"table_filter");
        }else {
            Util.addClass(body,"table_filter");
        }
    }

    render() {
        const { defaultActiveKey, orderStatus, orderType } = this.state;
        const { partA, oid, partB } = this.state.order
        const a1 = (partA == this.enterpriseId && (orderStatus == 1 || orderStatus == 2 || orderStatus == 3))   //采购订单 待签订 已签订
        const a2 = ((partA != this.enterpriseId) && orderStatus == 3)   // 销售  已签订
        const a3 = (partA != this.enterpriseId && orderStatus >= 3 && (partB != this.enterpriseId))   // 已失效
        const isPartA = (partA == this.enterpriseId);
        return (
          <div className="order_index">
              <br/>
            {
              a3 ? (
                <Tabs defaultActiveKey={defaultActiveKey.toString()} onChange={this.callback}>
                    <TabPane tab="订单详情" key="1">
                        <Index enquiryId={this.enquiryId} oid={this.orderId} isValid={this.state.isValid}/>
                    </TabPane>
                </Tabs>
              ) : (
                <Tabs defaultActiveKey={defaultActiveKey.toString()} onChange={this.callback}>
                    <TabPane tab="订单详情" key="1">
                        <Index enquiryId={this.enquiryId} oid={this.orderId} isValid={this.state.isValid}/>
                    </TabPane>
                    {
                        a1 || a2 || orderStatus == 4 ? (
                            <TabPane tab="合同管理" key="2">
                               <ContractManagement enquiryId={this.enquiryId} orderDetail={this.componentWillMount.bind(this)} orderStatus={orderStatus} orderId={this.orderId} />
                            </TabPane>
                        ) : null
                    }
                    {
                        //制衣厂&&成衣订单 && 销售订单 && 已签订或已完成
                        this.serviceType == 1 && orderType==1 && (orderStatus == 3 || orderStatus == 4 ) && this.enterpriseId == partB ? (
                            <TabPane tab="物料清单" key="9">
                                <MaterielBom
                                    orderId={this.orderId}
                                    orderStatus={orderStatus}
                                    orderType={orderType}
                                    enquiryId={this.enquiryId}
                                    account={this.account}
                                    partAName={this.partAName}
                                />
                            </TabPane>
                        ) : null
                    }
                    {
                        orderType==1 && orderStatus >= 3 ? (
                            <TabPane tab="打样进度" key="10">
                                <ProofingProgress
                                    isPartA={isPartA}
                                    partA={partA}
                                    ref={'proofing_progress'}
                                    orderId={this.orderId}
                                    enquiryId={this.enquiryId}
                                    orderClassify={this.state.orderClassify}
                                />
                            </TabPane>
                        ) : null
                    }
                    {
                        this.serviceType == 6 && orderType == 1 &&  orderStatus >= 3 ? (
                            <TabPane tab="面辅料进度" key="3">
                                <FabricRaw orderId={this.orderId}/>
                            </TabPane>
                        ) : null
                    }
                    {
                        this.serviceType == 6 && orderType == 1 && orderStatus >= 3 ? (
                            <TabPane tab="生产进度" key="4">
                                <OrderProductionProcess partb={this.state.order.partB || ''} orderId={this.orderId}/>
                            </TabPane>
                        ) : null
                    }
                    {
                       orderStatus == 3 || orderStatus == 4 ? (
                            <TabPane tab={partA == this.enterpriseId ? '收货记录' : '发货记录'} key="5">
                                <InOutOrder
                                    orderId={this.orderId}
                                    orderStatus={partA == this.enterpriseId ? "0" : "1"}
                                />
                            </TabPane>
                        ) : null
                    }
                    {
                        orderStatus == 3 || orderStatus == 4 ? (
                            <TabPane tab={partA == this.enterpriseId ? '付款记录' : '收款记录'} key="6">
                                <Payment
                                    orderId={this.orderId}
                                />
                            </TabPane>
                        ) : null
                    }
                    {/*{
                        orderStatus == 3  || orderStatus == 4 ? (
                            <TabPane tab="订单进度" key="7">
                                <OrderProgress
                                    orderId={this.orderId}
                                />
                            </TabPane>
                        ) : null
                    }*/}
                    {
                        partA == this.enterpriseId ? (
                            <TabPane tab="订单修改记录" key="8">
                                 <OrderModificationRecord
                                     orderId={this.orderId}
                                     enquiryId={this.enquiryId}
                                 />
                            </TabPane>
                        ) : null
                    }
                </Tabs>
              )
            }
          </div>
      )
    }
}
